package ui;


import java.io.IOException;

import exceptions.NotEnoughSharesToSell;
import exceptions.WrongPlayerName;
import exceptions.WrongShareName;
import mainBoerse.AccountManagerImpl;
import interfaces.AccountManager;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CommandReaderUI extends Application {
	private AccountManager acc;
	private static Label errLabel;
	
	public CommandReaderUI() throws IOException {
		this.acc = AccountManagerImpl.getInstance();
		CommandReaderUI.errLabel = new Label();
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("BoersenSpiel - CommandReader");
		
		//Menu
		MenuBar menuBar = new MenuBar();
		Menu menuMain = new Menu("Main");
		Menu menuAbout = new Menu("Help");
		menuBar.getMenus().addAll(menuMain, menuAbout);
		
		MenuItem about = new MenuItem("About");
		
		about.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Stage stage = new Stage();
				AboutWindow about = new AboutWindow();
				try {
					about.start(stage);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		MenuItem crp = new MenuItem("Create Player");
		crp.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Stage stage = new Stage();
				try {
					CreatePlayerWindow crpW = new CreatePlayerWindow();
					crpW.start(stage);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		MenuItem startAgent = new MenuItem("Start Player Agent");
		startAgent.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Stage stage = new Stage();
				
					StartAgentWindow sa;
					try {
						sa = new StartAgentWindow();
						sa.start(stage);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
				
			}
		});
		
		MenuItem exit = new MenuItem("Exit");
		exit.setOnAction(new EventHandler<ActionEvent>() {
		    public void handle(ActionEvent t) {
		        System.exit(0);
		    }
		});
		menuMain.getItems().addAll(crp,startAgent,exit);
		menuAbout.getItems().add(about);
		//Grid
		GridPane grid = new GridPane();
		grid.setPadding(new Insets(10, 10, 10, 10));
		grid.setVgap(3);
		grid.setHgap(6);
		
		Label cmdLabel = new Label("Enter parameters: FirstLine: Player  Second: ShareName  Third: Amount (int)");
		
		grid.add(cmdLabel,0,1,3,1);
		TextField playerName = new TextField();
		playerName.setPrefWidth(120);
		//grid.add(cmdLabel, 0, 1);
		grid.add(playerName, 0, 2);
		
		TextField shareName = new TextField();
		shareName.setPrefWidth(120);
		grid.add(shareName, 1, 2);
		
		TextField amount = new TextField();
		amount.setPrefWidth(120);
		grid.add(amount, 2, 2);
		
		Label commandRespondLabel = new Label();
		grid.add(commandRespondLabel, 0, 3);
		
		Button enterBuy = new Button("Buy");
		grid.add(enterBuy,0,4);
		enterBuy.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
					
				String labelText = handleCommands("bs",playerName.getText().toUpperCase(),shareName.getText().toUpperCase(),amount.getText());
				commandRespondLabel.setTextFill(Color.FIREBRICK);
				commandRespondLabel.setText(labelText);
				
			}
		});
		
		
		Button enterSell = new Button("Sell");
		grid.add(enterSell,1,4);
		enterSell.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				String labelText = handleCommands("ss",playerName.getText().toUpperCase(),shareName.getText().toUpperCase(),amount.getText());
				commandRespondLabel.setTextFill(Color.FIREBRICK);
				commandRespondLabel.setText(labelText);
				
			}
		});
		
		grid.add(errLabel, 0, 6,3,1);
		
		Scene scene = new Scene(new VBox(),600,300);
		((VBox) scene.getRoot()).getChildren().addAll(menuBar,grid);
		primaryStage.setScene(scene);
		primaryStage.show();
		
	}
	
	public static void setErrLabel(String string) {
		errLabel.setText(string);
	}
	
	private String handleCommands(String command, String...params) {
		switch(command) {
		case "bs": if(params.length != 3) return "Wrong number of Arguments";
			try {
				this.acc.buyShare(params[0], params[1], Integer.parseInt(params[2]));
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
				return e1.getMessage();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				return e1.getMessage();
			}
			break;
		case "ss": if(params.length != 3) return "Wrong number of Arguments";
			try {
				this.acc.sellShare(params[0], params[1], Integer.parseInt(params[2]));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				return e.getMessage();
			} catch (WrongPlayerName e) {
				// TODO Auto-generated catch block
				return e.getMessage();
			} catch (NotEnoughSharesToSell e) {
				// TODO Auto-generated catch block
				return e.getMessage();
			} catch (WrongShareName e) {
				// TODO Auto-generated catch block
				return e.getMessage();
			}
		break;
		default: return "unknown error";
		}
		return "success";
	}

}
